package com.alex.megacj.scenario;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;

import com.alex.megacj.alias.AliasManager;
import com.alex.megacj.filter.ContentFilter;
import com.alex.megacj.filter.RequestFilter;
import com.alex.megacj.handler.RequestContext;
import com.alex.megacj.mapper.URLMapper;

public abstract class Scenario implements InitializingBean {

	private AliasManager aliasManager;

	private Map<String, String> aliases;

	private String mirrorHost;

	private List<ContentFilter> contentFilters = Collections.emptyList();
	
	private List<RequestFilter> requestFilters = Collections.emptyList();

	private String sellURL;

	public List<ContentFilter> getContentFilters() {
		return contentFilters;
	}

	public void setContentFilters(List<ContentFilter> filters) {
		this.contentFilters = filters;
	}
	
	public void setRequestFilters(List<RequestFilter> requestFilters) {
		this.requestFilters = requestFilters;
	}
	
	public List<RequestFilter> getRequestFilters() {
		return requestFilters;
	}
	
	@Required
	public void setSellURL(String sellPage) {
		this.sellURL = sellPage;
	}
	
	public String getSellURL() {
		return sellURL;
	}

	@Required
	public void setMirrorHost(String mirrorHost) {
		this.mirrorHost = mirrorHost;
	}

	@Required
	public void setAliasManager(AliasManager aliasManager) {
		this.aliasManager = aliasManager;
	}

	public void setAliases(Map<String, String> aliases) {
		this.aliases = aliases;
	}

	public String getMirrorHost() {
		return mirrorHost;
	}

	public abstract boolean isAllowedHost(String host);

	public abstract URLMapper getUrlMapper();

	public abstract String simpifyURL(RequestContext context);

	@Override
	public void afterPropertiesSet() throws Exception {
		if (aliases != null) {
			for (Map.Entry<String, String> entry : aliases.entrySet()) {
				aliasManager.addAlias(entry.getKey(), entry.getValue() + "." + mirrorHost);
			}
		}
	}
}
